import 'dart:core';
import 'package:flutter/material.dart';
import 'package:common_utils/common_utils.dart';
import 'package:wowhy/service_locator.dart';
import 'package:wowhy/services/navigate_service.dart';

class SplashScreen extends StatefulWidget {
  final int seconds;
  final Color backgroundColor;
  final dynamic navigateAfterSeconds;
  final dynamic onClick;
  final ImageProvider imageBackground;
  final Gradient gradientBackground;
  final Widget child;

  SplashScreen({
    this.seconds = 3,
    this.onClick,
    this.navigateAfterSeconds,
    this.backgroundColor = Colors.white,
    this.imageBackground,
    this.gradientBackground,
    this.child,
  });

  @override
  _SplashScreenState createState() => _SplashScreenState();
}

class _SplashScreenState extends State<SplashScreen> {
  TimerUtil _countDownTimer;
  int _count;

  @override
  void initState() {
    super.initState();
    _countDown();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: InkWell(
        onTap: widget.onClick,
        child: Stack(
          children: <Widget>[
            if (widget.child == null)
              Container(
                padding: EdgeInsets.all(30.0),
                decoration: BoxDecoration(
                  image: widget.imageBackground == null
                      ? null
                      : DecorationImage(
                          fit: BoxFit.cover,
                          image: widget.imageBackground,
                        ),
                  gradient: widget.gradientBackground,
                  color: widget.backgroundColor,
                ),
              ),
            if (widget.child != null) widget.child,
            InkWell(
              onTap: _close,
              child: Container(
                width: 60,
                margin: EdgeInsets.only(top: 50.0, left: 300.0),
                padding: EdgeInsets.all(5),
                decoration: BoxDecoration(
                  color: Colors.black38,
                  borderRadius: BorderRadius.circular(5.0),
                ),
                child: Text(
                  "跳过 $_count",
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    fontSize: 14.0,
                    color: Colors.white,
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
    if (_countDownTimer != null) {
      this._countDownTimer.cancel();
      this._countDownTimer = null;
    }
  }

  void _countDown() {
    _count = widget.seconds;
    _countDownTimer = TimerUtil(
      mInterval: 1000,
      mTotalTime: widget.seconds * 1000 + 1000,
    );
    _countDownTimer.setOnTimerTickCallback((int tick) {
      int _tick = tick ~/ 1000;
      if (_tick == 0) {
        _close();
      } else {
        if (mounted) {
          setState(() {
            _count = _tick - 1;
          });
        }
      }
    });
    _countDownTimer.startCountDown();
  }

  void _close() {
    _countDownTimer.cancel();
    _countDownTimer = null;

    NavigateService navigator = getIt<NavigateService>();

    if (widget.navigateAfterSeconds is String) {
      // It's fairly safe to assume this is using the in-built material
      // named route component
      navigator.pushReplacementNamed(widget.navigateAfterSeconds);
    } else if (widget.navigateAfterSeconds is Widget) {
      navigator.pushReplacement(new MaterialPageRoute(
          builder: (BuildContext context) => widget.navigateAfterSeconds));
    } else if (widget.navigateAfterSeconds is Function) {
      widget.navigateAfterSeconds(context);
    } else {
      throw new ArgumentError(
          'widget.navigateAfterSeconds must either be a String or Widget');
    }
  }
}
